package nc.ui.ct.purdaily.action;

import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.List;

import nc.bs.framework.common.NCLocator;
import nc.itf.ct.purdaily.IPuContrl;
import nc.ui.ct.action.HelpAction;
import nc.ui.ct.purdaily.view.CtCtrlScopeDlg;
import nc.ui.ct.purdaily.view.PurdailyBillForm;
import nc.ui.pub.beans.UIDialog;
import nc.ui.scmpub.action.SCMActionInitializer;
import nc.ui.uif2.UIState;
import nc.vo.ct.purdaily.entity.AggCtPuVO;
import nc.vo.ct.purdaily.entity.CtScopeVo;
import nc.vo.ct.rule.ActionStateRule;
import nc.vo.ct.uitl.ArrayUtil;
import nc.vo.ct.uitl.ValueUtil;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.scmpub.res.SCMActionCode;

/**
 * <p>
 * 采购合同合同控制范围action <b>本类主要完成以下功能：</b>
 * <ul>
 * <li>
 * </ul>
 * <p>
 * <p>
 * 
 * @version 6.0
 * @since 6.0
 * @author lizhengb
 * @time 2010-5-11 下午01:17:13
 */
public class PuContrlAction extends HelpAction {
	/**
     *
     */
	private static final long serialVersionUID = 7263688937825522309L;

	private PurdailyBillForm cardForm = null;

	private CtCtrlScopeDlg m_dlgCtrlScope = null;

	public PuContrlAction() {
		SCMActionInitializer.initializeAction(this,
				SCMActionCode.CT_CTCONTROLSCOPE);
	}

	@Override
	public void doAction(ActionEvent e) throws Exception {
		AggCtPuVO aggvo = (AggCtPuVO) this.getModel().getSelectedData();
		if (ValueUtil.isEmpty(aggvo)
				|| ValueUtil.isEmpty(aggvo.getParentVO().getPk_ct_pu())) {
			ExceptionUtils
					.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl
							.getNCLangRes().getStrByID("4020003_0",
									"04020003-0053")/*
													 * @res
													 * "请选择需要查看或修改合同控制范围的合同! "
													 */);
		}
		// 判断是否是总括订单
		this.checkBracketOrder(aggvo);
		this.initCtCtrlScope(aggvo.getParentVO().getPk_ct_pu());
	}

	public PurdailyBillForm getCardForm() {
		return this.cardForm;
	}

	public CtCtrlScopeDlg getM_dlgCtrlScope() {
		if (this.m_dlgCtrlScope == null) {
			this.m_dlgCtrlScope = new CtCtrlScopeDlg(this.getModel()
					.getContext().getPk_group());
		}
		return this.m_dlgCtrlScope;
	}

	public void setCardForm(PurdailyBillForm cardForm) {
		this.cardForm = cardForm;
	}

	public void setM_dlgCtrlScope(CtCtrlScopeDlg ctrlScope) {
		this.m_dlgCtrlScope = ctrlScope;
	}

	private void checkBracketOrder(AggCtPuVO aggvo) {
		UFBoolean bBracketOrder = aggvo.getParentVO().getBbracketOrder();
		if (bBracketOrder.booleanValue()) {
			ExceptionUtils
					.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl
							.getNCLangRes().getStrByID("4020003_0",
									"04020003-0302")/*
													 * @res "总括订单不允许编辑合同控制范围"
													 */);
		}
	}

	/**
	 * 方法功能描述：简要描述本方法的功能。
	 * 
	 * @author wangfengd
	 * @time 2010-5-28 下午02:41:43
	 */
	private void initCtCtrlScope(String pk_ct_pu) throws Exception {
		List<String> sCtrlScope = new ArrayList<String>();
		// 远程调用
		IPuContrl service = NCLocator.getInstance().lookup(IPuContrl.class);
		CtScopeVo[] vos = service.queryCtScope(pk_ct_pu);
		// 除合同删除外 其他都可查询出控制范围
		if (ArrayUtil.isEmpty(vos)) {
			ExceptionUtils
					.wrappBusinessException(nc.vo.ml.NCLangRes4VoTransl
							.getNCLangRes().getStrByID("4020003_0",
									"04020003-0054")/*
													 * @res "合同可能已经被删除，请刷新合同 "
													 */);
			return;
		}
		for (int i = 0; i < vos.length; i++) {
			sCtrlScope.add(vos[i].getPk_scopeorg());
		}
		// 只有在自由状态，生效，审核状态 控制范围才可修改
		boolean isCtrlScopeEditable = false;
		ActionStateRule rule = new ActionStateRule();

		if (rule.isHaveFree(this.getModel().getSelectedData())
				|| rule.isHaveValidate(this.getModel().getSelectedData())
				|| rule.isHaveApprove(this.getModel().getSelectedData())
				|| rule.isHaveApproving(this.getModel().getSelectedData())) {
			isCtrlScopeEditable = true;
		} else {
			isCtrlScopeEditable = false;
		}
		// 获得合同控制范围 ，初始化左右List列表
		this.getM_dlgCtrlScope().initCorpList(
				sCtrlScope.toArray(new String[sCtrlScope.size()]),
				isCtrlScopeEditable);
		this.getM_dlgCtrlScope().getM_sCtCtrlScope();
		Object[] leftdata = this.getM_dlgCtrlScope().getLtlCorp2Corpp()
				.getLeftData();
		Object[] rightdata = this.getM_dlgCtrlScope().getLtlCorp2Corpp()
				.getRightData();
		Object[] newleftdata = this.sort(leftdata);
		Object[] newrightdata = this.sort(rightdata);
		this.getM_dlgCtrlScope().getLtlCorp2Corpp()
		.setLeftData(newleftdata);
		this.getM_dlgCtrlScope().getLtlCorp2Corpp()
		.setRightData(newrightdata);
		

		if (this.getM_dlgCtrlScope().showModal() == UIDialog.ID_OK) {
			this.updateCtCtrlScope(pk_ct_pu, vos,
					sCtrlScope.toArray(new String[sCtrlScope.size()]));
		}

	}

	private Object[] sort(Object[] strs) {
		if (strs == null || strs.length < 1) {
			return new String[0];
		}
		Object temp;

		for (int i = 1; i < strs.length; i++) {

			for (int j = 0; j < strs.length - i; j++) {

				if (strs[j].toString().compareTo(strs[j+1].toString()) > 0) {

					temp = strs[j];

					strs[j] = strs[j+1];

					strs[j+1] = temp;

				}

			}
		}
		return strs;

	}

	/**
	 * 方法功能描述：更新控制范围。
	 * 
	 * @param pk_ct_pu
	 * @author wangfengd
	 * @time 2010-5-28 上午09:11:13
	 */
	private void updateCtCtrlScope(String pk_ct_pu, CtScopeVo[] vos,
			String[] sCtrlScopeOld) {

		// 得到修改后的合同控制范围
		String[] sCtrlScopeNew;
		try {
			sCtrlScopeNew = this.getM_dlgCtrlScope().getM_sCtCtrlScope();
			if (sCtrlScopeNew != null && sCtrlScopeNew.length > 0) {
				// 远程调用
				IPuContrl service = NCLocator.getInstance().lookup(
						IPuContrl.class);
				service.updateCtCtrlScope(pk_ct_pu, sCtrlScopeNew,
						sCtrlScopeOld, vos);
			}
		} catch (BusinessException e) {
			ExceptionUtils.wrappException(e);
		}
	}

	@Override
	protected boolean isActionEnable() {
		AggCtPuVO aggvo = (AggCtPuVO) this.getModel().getSelectedData();
		if (ValueUtil.isEmpty(aggvo)
				|| ValueUtil.isEmpty(this.getModel().getSelectedOperaDatas())) {
			return false;
		}
		if (this.getModel().getUiState() == UIState.ADD
				|| this.getModel().getUiState() == UIState.EDIT) {
			return false;
		}
		return true;
	}
}
